home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / externaltools / capture.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.9 KB  |  111 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = ('Capture',)
  5. import os
  6. import sys
  7. import signal
  8. import locale
  9. import subprocess
  10. import gobject
  11.  
  12. class Capture(gobject.GObject):
  13.     CAPTURE_STDOUT = 1
  14.     CAPTURE_STDERR = 2
  15.     CAPTURE_BOTH = 3
  16.     __gsignals__ = {
  17.         'stdout-line': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)),
  18.         'stderr-line': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)),
  19.         'begin-execute': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, tuple()),
  20.         'end-execute': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT,)) }
  21.     
  22.     def __init__(self, command, cwd = None, env = { }):
  23.         gobject.GObject.__init__(self)
  24.         self.pipe = None
  25.         self.env = env
  26.         self.cwd = cwd
  27.         self.flags = self.CAPTURE_BOTH
  28.         self.command = command
  29.         self.input_text = None
  30.  
  31.     
  32.     def set_env(self, **values):
  33.         self.env.update(**values)
  34.  
  35.     
  36.     def set_command(self, command):
  37.         self.command = command
  38.  
  39.     
  40.     def set_flags(self, flags):
  41.         self.flags = flags
  42.  
  43.     
  44.     def set_input(self, text):
  45.         self.input_text = text
  46.  
  47.     
  48.     def set_cwd(self, cwd):
  49.         self.cwd = cwd
  50.  
  51.     
  52.     def execute(self):
  53.         if self.command is None:
  54.             return None
  55.         popen_args = {
  56.             'cwd': self.cwd,
  57.             'shell': True,
  58.             'env': self.env }
  59.         if self.input_text is not None:
  60.             popen_args['stdin'] = subprocess.PIPE
  61.         
  62.         if self.flags & self.CAPTURE_STDOUT:
  63.             popen_args['stdout'] = subprocess.PIPE
  64.         
  65.         if self.flags & self.CAPTURE_STDERR:
  66.             popen_args['stderr'] = subprocess.PIPE
  67.         
  68.         self.pipe = subprocess.Popen(self.command, **popen_args)
  69.         self.emit('begin-execute')
  70.         if self.input_text is not None:
  71.             self.pipe.stdin.write(self.input_text)
  72.             self.pipe.stdin.close()
  73.         
  74.         if self.flags & self.CAPTURE_STDOUT:
  75.             gobject.io_add_watch(self.pipe.stdout, gobject.IO_IN | gobject.IO_HUP, self.on_output)
  76.         
  77.         if self.flags & self.CAPTURE_STDERR:
  78.             gobject.io_add_watch(self.pipe.stderr, gobject.IO_IN | gobject.IO_HUP, self.on_output)
  79.         
  80.         gobject.child_watch_add(self.pipe.pid, self.on_child_end)
  81.  
  82.     
  83.     def on_output(self, source, condition):
  84.         line = source.readline()
  85.         if len(line) > 0:
  86.             
  87.             try:
  88.                 line = unicode(line, 'utf-8')
  89.             except:
  90.                 line = unicode(line, locale.getdefaultlocale()[1], 'replace')
  91.  
  92.             if source == self.pipe.stdout:
  93.                 self.emit('stdout-line', line)
  94.             else:
  95.                 self.emit('stderr-line', line)
  96.             return True
  97.         return False
  98.  
  99.     
  100.     def stop(self, error_code = -1):
  101.         if self.pipe is not None:
  102.             os.kill(self.pipe.pid, signal.SIGTERM)
  103.             self.pipe = None
  104.         
  105.  
  106.     
  107.     def on_child_end(self, pid, error_code):
  108.         gobject.idle_add(self.emit, 'end-execute', error_code)
  109.  
  110.  
  111.